package com.lwy.qiuchao.vending.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.lwy.common.utils.R;
import com.lwy.qiuchao.vending.feign.WareFeignService;
import com.lwy.qiuchao.vending.vo.SkuVo;
import com.lwy.qiuchao.vending.vo.SkuinfoVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.common.utils.PageUtils;
import com.lwy.common.utils.Query;

import com.lwy.qiuchao.vending.dao.VendingSkuDao;
import com.lwy.qiuchao.vending.entity.VendingSkuEntity;
import com.lwy.qiuchao.vending.service.VendingSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("vendingSkuService")
public class VendingSkuServiceImpl extends ServiceImpl<VendingSkuDao, VendingSkuEntity> implements VendingSkuService {

    @Autowired
    WareFeignService wareFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<VendingSkuEntity> page = this.page(
                new Query<VendingSkuEntity>().getPage(params),
                new QueryWrapper<VendingSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public R saveVending(VendingSkuEntity vendingSku) {
        System.out.println("---"+vendingSku);
        if(vendingSku!=null){
            //先删除在保存
            R r = wareFeignService.deleteNumbyskuid(vendingSku.getSkuId(), vendingSku.getSkuCount());
            if(r.getcode() == 0){
                VendingSkuEntity one = getOne(new QueryWrapper<VendingSkuEntity>().eq("vend_id", vendingSku.getVendId()).and(item -> {
                    item.eq("field_id", vendingSku.getFieldId())
                    .and(obj->{obj.eq("sku_id",vendingSku.getSkuId());});
                }));
                if(one==null){
                    save(vendingSku);
                }else{
                    baseMapper.updateCount(vendingSku.getVendId(),vendingSku.getFieldId(),vendingSku.getSkuId(),vendingSku.getSkuCount());
                }

            }
            return r;
            }
        else{
            return R.error(1002,"操作有误");
        }
    }

    @Override
    public List<SkuinfoVo> listdetail(Map<String, Object> params) {
        String s = (String) params.get("fieldId");
        Long fieldId = new Long(s);
        String s1 = (String) params.get("vendId");
        Long vendId = new Long(s1);
        String key = (String) params.get("key");
        QueryWrapper<VendingSkuEntity> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isEmpty(key)){
            List<VendingSkuEntity> entities = baseMapper.selectList(queryWrapper.eq("field_id", fieldId).and(item -> {
                item.eq("vend_id", vendId);
            }));
            return getSkuList(entities);

        }else{
            //todo
            List<VendingSkuEntity> entities = baseMapper.selectList(queryWrapper.eq("field_id", fieldId).and(item -> {
                item.eq("vend_id", vendId).and(obj -> {
                });
            }));
            return getSkuList(entities);
        }
    }

    /**
     * 通过VendingSkuEntity列表查询所含sku的具体信息
     * */
    private List<SkuinfoVo> getSkuList(List<VendingSkuEntity> entities) {
        List<SkuinfoVo> vos = entities.stream().map(entity -> {
            SkuinfoVo skuinfoVo = new SkuinfoVo();
            Long skuId = entity.getSkuId();
            R r = wareFeignService.info(skuId);
            if (r.getcode() == 0) {
                SkuVo skuInfo = r.getData("skuInfo", new TypeReference<SkuVo>() {
                });
                skuinfoVo.setSkuName(skuInfo.getSkuName());
                skuinfoVo.setPrice(skuInfo.getPrice());
                skuinfoVo.setSkuImg(skuInfo.getSkuImg());
                skuinfoVo.setSkuId(skuId);
                skuinfoVo.setSkuCount(entity.getSkuCount());
            }
            return skuinfoVo;
        }).collect(Collectors.toList());

    return vos;
    }


}